package com.bins.dentist.clinic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bins.dentist.clinic.constants.CommonConstants;
import com.bins.dentist.clinic.entity.PatientEntity;
import com.bins.dentist.clinic.mapper.PatientMapper;
import com.bins.dentist.clinic.model.dto.PatientDto;
import com.bins.dentist.clinic.model.dto.PatientPageDto;
import com.bins.dentist.clinic.model.vo.PatientVo;
import com.bins.dentist.clinic.model.vo.SimpleSelectVo;
import com.bins.dentist.clinic.service.PatientService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bins
 * @since 2023-04-23 13:22:37
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, PatientEntity> implements PatientService {

    @Autowired
    private PatientMapper patientMapper;

    @Override
    public IPage<PatientVo> getPage(PatientPageDto pageDto) {
        LambdaQueryWrapper<PatientEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(!ObjectUtils.isEmpty(pageDto.getPatientCode()), PatientEntity::getPatientCode, pageDto.getPatientCode())
                .like(!ObjectUtils.isEmpty(pageDto.getPatientName()), PatientEntity::getPatientName, pageDto.getPatientName())
                .eq(!ObjectUtils.isEmpty(pageDto.getMobile()), PatientEntity::getMobile, pageDto.getMobile())
                .eq(!ObjectUtils.isEmpty(pageDto.getMedicalRecordNo()), PatientEntity::getMedicalRecordNo, pageDto.getMedicalRecordNo())
                .eq(PatientEntity::getIsDel, CommonConstants.DELETED_NO);
        IPage page = patientMapper.selectPage(new Page(pageDto.getPageNum(), pageDto.getPageSize()), queryWrapper);
        List<PatientEntity> pageList = page.getRecords();
        if (CollectionUtils.isEmpty(pageList)) {
            return page;
        }
        List<PatientVo> dateList = Lists.newArrayListWithCapacity(pageList.size());
        for (PatientEntity entity : pageList) {
            PatientVo itemVo = new PatientVo();
            BeanUtils.copyProperties(entity, itemVo);
            dateList.add(itemVo);
        }
        page.setRecords(dateList);
        return page;
    }

    @Override
    public PatientVo getDetail(PatientDto dto) {
        PatientEntity patientEntity = patientMapper.selectById(dto.getId());
        PatientVo resultVo = new PatientVo();
        BeanUtils.copyProperties(patientEntity, resultVo);
        return resultVo;
    }

    @Override
    public PatientVo savePatient(PatientDto dto) {
        PatientEntity patientEntity = new PatientEntity();
        BeanUtils.copyProperties(dto, patientEntity);
        patientEntity.setUpdateBy(0l);
        patientEntity.setUpdateTime(LocalDateTime.now());
        if (ObjectUtils.isEmpty(dto.getId())) {
            patientEntity.setIsDel(CommonConstants.DELETED_NO);
            patientEntity.setCreateBy(0l);
            patientEntity.setCreateTime(LocalDateTime.now());
            patientMapper.insert(patientEntity);
        } else {
            patientMapper.updateById(patientEntity);
        }
        PatientVo resultVo = new PatientVo();
        resultVo.setId(patientEntity.getId());
        return resultVo;
    }

    @Override
    public Integer deletePatient(PatientDto dto) {
        PatientEntity existEntity = patientMapper.selectById(dto.getId());
        PatientEntity entity = new PatientEntity();
        entity.setId(dto.getId());
        entity.setIsDel(CommonConstants.DELETED_YES);
        return patientMapper.updateById(entity);
    }

    @Override
    public List<SimpleSelectVo> selectList(PatientDto dto) {
        LambdaQueryWrapper<PatientEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(PatientEntity::getIsDel, CommonConstants.DELETED_NO);
        List<PatientEntity> queryList = patientMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(queryList)) {
            return Collections.emptyList();
        }
        List<SimpleSelectVo> resultList = Lists.newArrayListWithCapacity(queryList.size());
        SimpleSelectVo simpleSelectVo;
        for (PatientEntity itemEntity : queryList) {
            simpleSelectVo = new SimpleSelectVo();
            simpleSelectVo.setId(itemEntity.getId());
            simpleSelectVo.setName(itemEntity.getPatientName());
            resultList.add(simpleSelectVo);
        }
        return resultList;
    }
}
